import operate from '@dfeidao/atom-nodejs/db/operate';
import query from '@dfeidao/atom-nodejs/db/query';
import log from '@dfeidao/atom-nodejs/logger/log';
import doc_code from '@dfeidao/atom-nodejs/msg/doc-code';
import sys_date from '@dfeidao/atom-nodejs/msg/sys-date';
import { IncomingHttpHeaders } from 'http';
import { systemid } from '../atom/config';
interface Message {
	teacher_no: string;
	teacher_name: string;
	class_id: string;
	class_name: string;
	subject_num: string;
	subject_name: string;
	quote_job_name: string;
	contents_homework: string;
	productid: string;
	// cookie: {
	// 	uk: string;
	// 	[key: string]: string
	// };
	// urls: {
	// 	base: string;
	// 	origin: string;
	// 	url: string;
	// };
	// query: {};
	// params: {};
	// headers: {};
	// captcha: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string[];
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:app-class-work/s002,action_id:' + action_id);
	const time = sys_date(action_id, session_id);
	const code = await doc_code(action_id, session_id, systemid, 'BM0016062', '', 1);
	const job_no1 = code.result;
	const { prepare, exec } = query(action_id, session_id, systemid);
	const { insert, exec: e } = operate(action_id, session_id, systemid);
	prepare('class', ['grade', 'grade_no'], { class_id: message.class_id, productid: message.productid }, 10, 1, [], []);
	prepare('class_student', ['student_no', 'student_name'], { class_id: message.class_id, productid: message.productid }, 199, 1, [], []);
	const [data, data1] = await exec<{ grade: string, grade_no: string }, { student_no: string, student_name: string }>();
	const grade1 = data[0].grade;
	const grade_no1 = data[0].grade_no;
	const student = new Array() as [{ student_no: string, student_name: string }];
	data1.forEach((i) => {
		student.push({ student_no: i.student_no, student_name: i.student_name });
	});
	insert('task', { job_no: job_no1, quote_job_name: message.quote_job_name, contents_homework: message.contents_homework, teacher_no: message.teacher_no, teacher_name: message.teacher_name, subject_num: message.subject_num, subject_name: message.subject_name, class_id: message.class_id, class_name: message.class_name, grade: grade1, grade_no: grade_no1, personalized_setting: '0', task_type: '0', create_time: time, productid: message.productid });
	const code1 = await doc_code(action_id, session_id, systemid, 'BM0016062', '', student.length);
	const detailnums = code1.results;
	student.forEach((i, index) => {
		insert('stu_task_detail', { stu_task_detail_num: detailnums[index], job_no: job_no1, quote_job_name: message.quote_job_name, contents_homework: message.contents_homework, student_no: i.student_no, student_name: i.student_name, job_status: '0', submission_time: time, reader: '', read_time: 0, read_concent: '', productid: message.productid, subject_num: message.subject_num, subject_name: message.subject_name, submit_contents: '', submit_status: '0' });
	});
	const ret = await e();
	log('Service end path:app-class-work/s002,action_id:' + action_id);
	return { data: ret } as IWebResult;
}
